package com.zk.algorithm.array;

import com.zk.algorithm.Utils;
import com.zk.algorithm.annotation.Easy;
import com.zk.algorithm.annotation.LeetCodeExplore;

/**
 * @author zk
 */
@LeetCodeExplore
@Easy
public class RotateArray {

    // [1,2,3,4,5,6,7] and k = 3
    // [5,6,7,1,2,3,4]
    //
    public void rotate(int[] nums, int k) {
        if (nums == null) {
            return;
        }

        k %= nums.length;

        swapArray(nums, 0, nums.length - k - 1);
        swapArray(nums, nums.length - k, nums.length - 1);
        swapArray(nums, 0, nums.length - 1);
    }

    private void swapArray(int[] nums, int i, int j) {
        while (i <= j) {
            Utils.swap(nums, i, j);
            i++;
            j--;
        }
    }

    public static void main(String...args) {
        int[] nums = new int[] {
                1, 2, 3, 4, 5, 6, 7
        };
//        new RotateArray().rotate(nums, 3);
        new RotateArray().rotate(nums, 0);
        new RotateArray().rotate(nums, 1);
        Utils.println(nums);
    }

}